
function fval = solve_1s_2c_gov_ss_nogL(xval)

% for multi-sectors
global rbest  L theta ns n alpha betas d sigma ilog_rL delta3 beta

% guess wage and share of labor in each sector




w(1:n,1) = xval(1:n); % row vector
extax = xval(n+1);
taf = 0;



rL = rbest*L;

T = alpha.*rL/delta3;

sextax = zeros(n);
for i=1:n-1
    sextax(i+1) = extax;
end
staf = zeros(n);

for i=1:n-1
    staf(i+1) = taf;
end

pi = zeros(n,n); % (1,2,3): country 2 export to ecountry 1 in sector 3, NOTE country order is opposite to our note
%d, matrix of n times n, dij country j ship to country i


xnimatrix1= T'.*((w'.*(1+sextax).*(1+staf).*d).^(-theta));
 xn1 =sum(xnimatrix1,2).*ones(n,n);

pimatrix1 = xnimatrix1./xn1;
pi = pimatrix1;
xnimatrix= xnimatrix1;


x  =  (1+theta)/theta*w.*(L-sum(rL,2));

extax1 = sextax;
for m=2:n
    x(1)=x(1)+betas(1)*extax/(1+extax)*pi(m,1)*x(m);
end
tempt=0;

for m=2:n
    tempt=tempt+betas(1)*taf/(1+taf)*pi(1,m);
end

x(1)=x(1)/(1-tempt);


P=(sum(xnimatrix1,2)).^(-betas./theta);

fval(1) = P(1)-1;
fval(2) = extax-1/(theta*pi(2,2));
fval(3) = pi(1,2)*x(1)-pi(2,1)*x(2);
